package algorithm.exam.array;

import java.util.Arrays;

public class maxProduct {

    public static int maxProduct(int[] nums) {
        int max =nums[0];

        return Productmax(max, max,Arrays.copyOfRange(nums,1,nums.length),true);




    }

    private static int Productmax(int max, int last,int[] newNums,boolean flag) {
        if (newNums.length==0){
            return max;
        }
        int newmax =0;
        if(flag){
             newmax =Math.max(Math.max(Math.max(Math.max(max,max*newNums[0]),last),newNums[0]),last*newNums[0]);
        }else {
            newmax =Math.max(Math.max(Math.max(max,last),newNums[0]),last*newNums[0]);
        }
       if(newmax==last||newmax==last*newNums[0]){
           return Productmax(newmax,newNums[0],Arrays.copyOfRange(newNums,1,newNums.length),true);
       }else {
           return Productmax(newmax,newNums[0],Arrays.copyOfRange(newNums,1,newNums.length),false);
       }





    }

    public static void main(String[] args) {
        int[] nums={3,-1,4};
        System.out.println(maxProduct(nums));;
    }


}
